import datetime
import pandas as pd
import numpy as np
import math
import statsmodels.api as sm
from sklearn.linear_model import LinearRegression
# import Analysis.IndustryAnalysis
import Core.Gadget as Gadget

def Get_Industry_Exposure(symbols, datetime2):
    # 找到最近的一次 Release Date
    releaseDate = Gadget.FindReleaseDate(datetime2)
    #
    filter = {"DateTime": releaseDate, "Type": "Citic"}
    documents = database.Find("Stock", "Industry", filter)
    dfSymbolwithIndustry = Gadget.DocumentsToDataFrame(documents, keep=["DateTime", "Symbol", "Industry"])
    #

    # symbols 和 df 可以Merge 一下

    return dfSymbolwithIndustry

def Neutralize(database, factorName, newFactorName, reportDate, betaFactors=["lnCap"], industry=False):
    # 读取因子-Y
    filter = {"ReportDate": reportDate}
    documents = database.Find("Factor", factorName, filter)
    dfYFactor = Gadget.DocumentsToDataFrame(documents, keep=["Value", "DateTime", "Symbol", "ReportDate", "Period"])

    # 读取因子-Xs （Beta Factor）

    # 确认行业信息
    dfSymbolIndustry = Get_Industry_Exposure(list(dfYFactor["Symbol"]), reportDate)


    # 建立回归模型


    # 取残差


    # 将新因子存到一个新的Table


if __name__ == '__main__':
    #
    from Core.Config import *
    cfgPathFilename = os.getcwd() + "/../config.json"
    config = Config(cfgPathFilename)
    database = config.DataBase("MySQL")

    reportDate = datetime.datetime(2018, 3, 31)
    factorName = "Roe_NetIncome2_TTM"

    df = Neutralize(database, factorName, factorName + "_Neutral", reportDate, betaFactors=["lnCap"], industry=True)